package dao;

import java.io.Serializable;
import java.util.List;

import modelo.Modopago;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class ModoPagoDao implements Serializable {

		private static final long serialVersionUID = -228135013147854766L;
	private Session session;
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	public ModoPagoDao() {
		super();
	}

	public ModoPagoDao(Session session, SessionFactory sessionFactory) {
		super();
		this.session = session;
	}

	public List<Modopago> obtenerModosPagos() {
		session = sessionFactory.openSession();

		Query query = session.createQuery("SELECT mp FROM Modopago mp");
		List<Modopago> c = query.list();

		session.close();

		return c;
	}

	public Modopago obtenerModoPagoById(int id) {
		session = sessionFactory.openSession();
		Query query = session
				.createQuery("from Modopago where IdModopago = :id ");
		query.setParameter("id", id);

		List<?> list = query.list();

		Modopago mp = (Modopago) list.get(0);
		return mp;
	}

	public void guardarModoPago(Modopago mp) {
		session = sessionFactory.openSession();
		session.beginTransaction();
		session.saveOrUpdate(mp);
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarConcepto(Modopago mp) {
		session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(mp);
		session.getTransaction().commit();
		session.close();
	}

}
